package com.huasheng.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.huasheng.entity.EmployeeEntity;
import com.huasheng.entity.SysUserEntity;
import com.huasheng.service.EmployeeService;
import com.huasheng.service.SysUserLoginLogService;
import com.huasheng.service.SysUserRoleService;
import com.huasheng.service.SysUserService;
import com.huasheng.util.PageUtils;
import com.huasheng.util.R;
import com.huasheng.util.ShiroUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 门店管理
 *
 * @author Joey
 * @Email wangguoqing_it@163.com
 */
@Controller
@RequestMapping("/admin/sys/emp")
public class EmployeeController extends AbstractController {

    @Resource
    private EmployeeService employeeService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:emp:list")
    @ResponseBody
    public R list(Integer offset, Integer limit, String sort, String order,
                  @RequestParam(name = "search", required = false) String search) {
        String userName = null;
        String email = null;
        Map<String, String> searchList = parseObject(search, "q_userName", "q_email");
        if (null != searchList) {
            userName = searchList.get("q_userName");
            email = searchList.get("q_email");
        }
        offset = (offset / limit) + 1;
        Boolean flag = null; // 排序逻辑
        if (StringUtils.isNoneBlank(order)) {
            if (order.equalsIgnoreCase("asc")) {
                flag = true;
            } else {
                flag = false;
            }
        }
        Page<EmployeeEntity> adminList = employeeService.queryListByPage(offset, limit, email, userName, sort, flag);
        PageUtils pageUtil = new PageUtils(adminList.getRecords(), adminList.getTotal(), adminList.getSize(),
                adminList.getCurrent());
        return R.ok().put("page", pageUtil);
    }

}
